package com.example.bnuzzu;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bnuzzu.entity.Orderinfo;
import com.example.bnuzzu.entity.Userinfo;
import com.example.bnuzzu.mapper.OrderinfoMapper;
import com.example.bnuzzu.service.UserinfoService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import com.example.bnuzzu.mapper.UserinfoMapper;

@SpringBootTest
class MybatisPlusExamples {
	/*
		官方文档地址
		https://mp.baomidou.com/
	 */
	@Autowired
	private UserinfoMapper userinfoMapper;

	@Autowired
	UserinfoService userinfoService;

	@Autowired
	private OrderinfoMapper orderMapper;
	
	@Test
	void contextLoads() {
		//List<User> user = userMapper.selectList(null);
		//user.forEach(System.out::println);

		HashMap<String,Object> map = new HashMap<>();
		map.put("UserName","admin");
		List<Userinfo> users = userinfoMapper.selectByMap(map);
		users.forEach(System.out::println);

	}
	@Test
	public void testInsert(){
		Userinfo theshy = new Userinfo();
		theshy.setUsername("theshy1");
 		theshy.setPassword(SecureUtil.md5("123"));
		theshy.setAge(21);
		theshy.setPhone("13333333");
		theshy.setEmail("topgod@ig.com");

		int result = userinfoMapper.insert(theshy);
		System.out.println(result);
		System.out.println(theshy);

//		Orderinfo order = new Orderinfo();
//		order.setOwnerid(1);
//		order.setTenantid(1);
//		order.setHouseid(1);
//
//		int result = orderMapper.insert(order);
//		System.out.println(order);
	}



	@Test
	public void testUpdate(){
		Userinfo userinfo = new Userinfo();
		userinfo.setUserid(1);
		userinfo.setAge(1);

		int i = userinfoMapper.updateById(userinfo);
		userinfoMapper.updateById(userinfo);
	}

	@Test
	public void testOpt(){
		Orderinfo order = orderMapper.selectById(1L);
		order.setOrderstatus(1);

		// 模拟另外一个线程执行了插队操作
		Orderinfo order1 = orderMapper.selectById(1L);
		order.setOrderstatus(2);
		orderMapper.updateById(order1);

		//1将会提交失败
		orderMapper.updateById(order);
	}

	// 测试批量查询
	@Test
	public void testSelectByBatchId(){
		List<Userinfo> users = userinfoMapper.selectBatchIds(Arrays.asList(1, 2, 3));
		users.forEach(System.out::println);
	}

	//测试分页查询
	@Test
	public void testPage(){
		//  参数一：当前页
		//  参数二：页面大小
		Page<Userinfo> page = new Page<>(2,5);
		userinfoMapper.selectPage(page,null);

		page.getRecords().forEach(System.out::println);
	}


	@Test
	public void testDelete(){
		/*
			如设置了逻辑删除，只会将逻辑删除变量设为1，不会真正删除
		 */

		//id删除
		//userMapper.deleteById(8);

		//map删除
		HashMap<String,Object> map = new HashMap<>();
		map.put("username","theshy");
		userinfoMapper.deleteByMap(map);
	}


	@Test
	public void testUpdatebyid(){
		Userinfo userinfo = new Userinfo();
		userinfo.setPassword("123");
		userinfoService.updateById(userinfo);
	}
}
